package com.debao.web.controller.debao;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.debao.system.domain.CourseCard;
import com.debao.system.domain.dto.TransferCardDto;
import com.debao.system.service.ICourseCardService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.debao.common.annotation.Log;
import com.debao.common.core.controller.BaseController;
import com.debao.common.core.domain.AjaxResult;
import com.debao.common.enums.BusinessType;
import com.debao.system.domain.UserCourseCard;
import com.debao.system.service.IUserCourseCardService;
import com.debao.common.utils.poi.ExcelUtil;
import com.debao.common.core.page.TableDataInfo;

/**
 * 用户的课程卡Controller
 * 
 * @author DeBao
 * @date 2024-06-19
 */
@RestController
@RequestMapping("/debao/userCourseCard")
public class UserCourseCardController extends BaseController
{
    @Autowired
    private IUserCourseCardService userCourseCardService;

    @Resource
    private ICourseCardService courseCardService;

    /**
     * 查询用户的课程卡列表
     */
    @PreAuthorize("@ss.hasPermi('debao:userCourseCard:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserCourseCard userCourseCard)
    {
        startPage();
        List<UserCourseCard> list = userCourseCardService.selectUserCourseCardList(userCourseCard);
        return getDataTable(list);
    }

    /**
     * 导出用户的课程卡列表
     */
    @PreAuthorize("@ss.hasPermi('debao:userCourseCard:export')")
    @Log(title = "用户的课程卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserCourseCard userCourseCard)
    {
        List<UserCourseCard> list = userCourseCardService.selectUserCourseCardList(userCourseCard);
        ExcelUtil<UserCourseCard> util = new ExcelUtil<UserCourseCard>(UserCourseCard.class);
        util.exportExcel(response, list, "用户的课程卡数据");
    }

    /**
     * 获取用户的课程卡详细信息
     */
    @PreAuthorize("@ss.hasPermi('debao:userCourseCard:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userCourseCardService.selectUserCourseCardById(id));
    }
    /**
     * 新增用户的课程卡
     */
    @PreAuthorize("@ss.hasPermi('debao:userCourseCard:add')")
    @Log(title = "用户的课程卡", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserCourseCard userCourseCard)
    {
        return toAjax(userCourseCardService.insertUserCourseCard(userCourseCard));
    }
    /**
     * 修改用户的课程卡
     */
    @PreAuthorize("@ss.hasPermi('debao:userCourseCard:edit')")
    @Log(title = "用户的课程卡", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserCourseCard userCourseCard)
    {
        return toAjax(userCourseCardService.updateUserCourseCard(userCourseCard));
    }

    /**
     * 删除用户的课程卡
     */
    @PreAuthorize("@ss.hasPermi('debao:userCourseCard:remove')")
    @Log(title = "用户的课程卡", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userCourseCardService.deleteUserCourseCardByIds(ids));
    }

    /**
     * 客户转移
     */
    @PutMapping("/transfer")
    public AjaxResult transfer(@RequestBody @Valid TransferCardDto dto){
        return toAjax(userCourseCardService.updateUserCourseCard(new UserCourseCard().setId(dto.getId()).setCourseCardId(dto.getCourseId()).setCoachId(dto.getCoachId())));
    }

    /**
     *
     * 根据教练id查询课程卡次卡列表
     */
    @GetMapping("/selectCourseCardList/{coachId}/{typeId}")
    public AjaxResult selectTimeUserCardListByCoachId(@PathVariable("coachId") Long coachId,@PathVariable("typeId") Long typeId) {
        List<CourseCard> courseCardList = courseCardService.selectCourseCardList(new CourseCard().setCoachId(coachId).setTypeId(typeId));
        return AjaxResult.success(courseCardList);
    }

}
